//
//  TWSHomeViewController.m
//  TWSWeibo
//
//  Created by Twisal on 2016/12/28.
//  Copyright © 2016年 Twisal. All rights reserved.
//

#import "TWSHomeViewController.h"
#import "TWSDemoViewController.h"
static NSString *cellId = @"cellId";

@interface TWSHomeViewController ()
@property (nonatomic, strong)NSMutableArray *statusList;
@end

@implementation TWSHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

/**
 加载数据
 */
- (void)loadData {
//    [super loadData];
    NSLog(@"开始加载数据");
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (NSInteger i = 0; i < 15; i++) {
            NSLog(@"正在加载中...");
            if (weakSelf.isPullup) {
                [weakSelf.statusList addObject:[NSString stringWithFormat:@"上拉--%zd", i]];
            } else {
                
                [weakSelf.statusList insertObject:[NSString stringWithFormat:@"%zd", i] atIndex:0];
            }
        }
        NSLog(@"...结束加载数据");
        [weakSelf.refreshControl endRefreshing];
        [weakSelf.tableView reloadData];
        weakSelf.isPullup = NO;
    });
}

/**
 监听事件
 */
- (void)showFriends {
    TWSDemoViewController *nextVc = [[TWSDemoViewController alloc] init];
    [self.navigationController pushViewController:nextVc animated:YES];
}

#pragma mark - 页面布局
- (void)setupUserView {
    [super setupUserView];
    self.naviItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"好友" target:self action:@selector(showFriends)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
}


#pragma mark - UITableViewDataSoure
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.statusList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    cell.textLabel.text = self.statusList[indexPath.row];
    return cell;
}


#pragma mark - 懒加载
- (NSMutableArray *)statusList {
    if (!_statusList) {
        _statusList = [NSMutableArray array];
    }
    return _statusList;
}

@end
